//
//  EntertainmentTableViewController.m
//  老司机发车了
//
//  Created by 程成 on 16/8/6.
//  Copyright © 2016年 斌. All rights reserved.
//

#import "EntertainmentTableViewController.h"
#import "FirstNewsModel.h"
#import "FirstNews1TableViewCell.h"
#import "FirstNews2TableViewCell.h"
#import "SpecialTableViewController.h"
#import "DetailCellViewController.h"
#import "CarouselModel.h"
@interface EntertainmentTableViewController ()
/*轮播图数组*/
@property (strong, nonatomic )NSMutableArray *carouselArray;
@property (nonatomic, strong)ZXRollView *scrollView;
/*imgV1*/
@property (strong, nonatomic )UIImageView *imgV1;
/*imgV2*/
@property (strong, nonatomic )UIImageView *imgV2;
/*label*/
@property (strong, nonatomic )UILabel *label;
/*news*/
@property (strong, nonatomic )NSMutableArray *newsArray;
//
@property (nonatomic, assign)NSInteger start;
@property (nonatomic, assign)NSInteger limit;
@end

@implementation EntertainmentTableViewController
- (NSMutableArray *)carouselArray{
    if (!_carouselArray) {
        _carouselArray = [[NSMutableArray alloc] init];
    }
    return _carouselArray;
}
- (NSMutableArray *)newsArray{
    if (!_newsArray) {
        _newsArray = [[NSMutableArray alloc] init];
    }
    return _newsArray;
}
- (void)analyseData{
    [NetWorkRequestManager requestWithType:GET urlString:EntertainmentUrl parDic:@{} finish:^(NSData *data) {
        NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
        if (_start == 0) {
            [self.newsArray removeAllObjects];
        }
        for (NSDictionary *dic in dictionary[@"T1348648517839"][0][@"ads"]) {
            CarouselModel *model = [[CarouselModel alloc ] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.carouselArray addObject:model];
        }
        for (NSDictionary *dic in dictionary[@"T1348648517839"]) {
            FirstNewsModel *model = [[FirstNewsModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.newsArray addObject:model];
        }
        [self.newsArray removeObjectAtIndex:0];

        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
            //结束刷新(在表视图刷新完毕之后)
            [self.tableView.mj_header endRefreshing];
        });
        //结束加载
        [self.tableView.mj_footer endRefreshing];
    } finishError:^(NSError *error) {
        NSLog(@"数据解析出错");
    }];
}

- (void)analysePullupRefresh{
    NSString *url = [NSString stringWithFormat:EntertainmentRefresh,_start];
    [NetWorkRequestManager requestWithType:GET urlString:url parDic:@{@"start":@(_start),@"limit":@(_limit)} finish:^(NSData *data) {
        NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
        if (_start == 0) {
            [self.newsArray removeAllObjects];
        }
        for (NSDictionary *dic in dictionary[@"T1348648517839"][0][@"ads"]) {
            CarouselModel *model = [[CarouselModel alloc ] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.carouselArray addObject:model];
        }

        for (NSDictionary *dic in dictionary[@"T1348648517839"]) {
            FirstNewsModel *model = [[FirstNewsModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.newsArray addObject:model];
        }
        [self.newsArray removeObjectAtIndex:0];

        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
            //结束刷新(在表视图刷新完毕之后)
            [self.tableView.mj_header endRefreshing];
        });
        //结束加载
        [self.tableView.mj_footer endRefreshing];
    } finishError:^(NSError *error) {
        NSLog(@"数据解析出错");
    }];
}
//下拉刷新
- (void)pullDownHeader{
    //开始下拉刷新
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        _start = 0;
        [self analyseData];
    }];
    //开始上拉加载
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        self.start = _start += 20;
        [self analysePullupRefresh];
    }];

}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self analyseData];
    [self pullDownHeader];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"FirstNews1TableViewCell" bundle:nil] forCellReuseIdentifier:@"fn1Cell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"FirstNews2TableViewCell" bundle:nil] forCellReuseIdentifier:@"fn2Cell"];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 1;
    }else
        return self.newsArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 180)];
        self.imgV1 = [[UIImageView alloc] initWithFrame:view.frame];
        [_imgV1 sd_setImageWithURL:[NSURL URLWithString:((CarouselModel *)_carouselArray[0]).imgsrc]];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
        _imgV1.userInteractionEnabled = YES;
        [_imgV1 addGestureRecognizer:tap];
        _imgV1.tag = 11;

        self.label = [[UILabel alloc] initWithFrame:CGRectMake(5, 0,self.view.bounds.size.width, 20)];
        _label.font = [UIFont systemFontOfSize:13];
        _label.backgroundColor = [UIColor grayColor];
        _label.alpha = 0.8;
        _label.textColor = [UIColor whiteColor];
        _label.text = ((CarouselModel *)_carouselArray[0]).title;
        [_imgV1 addSubview:_label];
        [view addSubview:_imgV1];
        [cell addSubview:view];
        return cell;
    }else{
        FirstNewsModel *model = self.newsArray[indexPath.row];
        if (![model.boardid isEqualToString:@"photoview_bbs"]) {
            FirstNews1TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"fn1Cell"];
            if (!cell) {
                cell = [[FirstNews1TableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"fn1Cell"];
            }

            [cell setValueWithModel1:model];
            if (![model.skipType isEqualToString:@"special"]) {
                cell.specialLabel.backgroundColor = [UIColor whiteColor];
            }else{
                cell.specialLabel.backgroundColor = [UIColor redColor];
            }
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            return cell;
        }else{
            FirstNews2TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"fn2Cell"];
            if (!cell) {
                cell = [[FirstNews2TableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"fn2Cell"];
            }
            [cell setValueWithModel2:model];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            return cell;
        }
    }
}
//点击方法
- (void)tapAction:(UITapGestureRecognizer *)tap{
    UIView *view = tap.view;
    NSInteger index = view.tag - 11;
    CarouselModel *model = self.carouselArray[index];
    DetailCellViewController *detailVC = [[DetailCellViewController alloc] init];
    detailVC.skipid = model.url;
    detailVC.index = 1;
    detailVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:detailVC animated:YES];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    FirstNewsModel *model = self.newsArray[indexPath.row];
    DetailCellViewController *detailVC = [[DetailCellViewController alloc] init];
    if ([model.skipType isEqualToString:@"special"]) {
        SpecialTableViewController *specialVC = [[SpecialTableViewController alloc] init];
        specialVC.specialid = model.skipID;
        [self.navigationController pushViewController:specialVC animated:YES];
    }else if([model.boardid isEqualToString:@"photoview_bbs"]){
        detailVC.skipid = model.skipID;
        detailVC.index = 3;
        detailVC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:detailVC animated:YES];
    }else{
        detailVC.urlString = model.url_3w;
        detailVC.index = 2;
        detailVC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:detailVC animated:YES];

    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return 180;
    }else{
        FirstNewsModel *model = self.newsArray[indexPath.row];
        if (![model.boardid isEqualToString:@"photoview_bbs"]) {
            return 100;
        }else{
            return 120;
        }
    }
}


@end
